<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
    <script language="JavaScript" src="/newmain/js/bizjs/gy.js"></script>
</head>
<body>
<div class="panel panel-default operation" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">方案名称</label>
                <div class="layui-input-block">
                    <input type="name" name="name" placeholder="请输入方案名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">方案类型</label>
                <div class="layui-input-block">
                    <select id="type"  name="type" lay-filter="type">
                        <option value="1">双随机选案</option>
                        <option value="2">重点行业</option>
                        <option value="3">转交办</option>
                        <option value="4">其它</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item" id="zdhyContainer" style="display:none;">
                <label class="layui-form-label">重点行业</label>
                <div class="layui-input-block">
                    <select id="typeZdhy" name="typeZdhy">
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">方案时间起</label>
                <div class="layui-input-block">
                    <input type="text" readonly class="layui-input" name="startTime" id="startTime" placeholder="方案时间起">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">方案时间止</label>
                <div class="layui-input-block">
                    <input type="text" readonly class="layui-input" name="endTime" id="endTime" placeholder="方案时间止">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">方案文本</label>
                <div class="layui-input-block">
                    <input type="content" name="content" placeholder="请输入方案文本" autocomplete="off" class="layui-textarea">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="bz" name="bz" placeholder="请输入备注" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table_div">
    <div id="searchParam"  hidden>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="key" class="layui-input"  autocomplete="off" placeholder="请输入">
            </div>

            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()"  id="search">查询</button>
                <button class="layui-btn"   id="export">导出全部</button>
            </div>
        </div>

    </div>
    <div id="showTable_div">
        <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
    </div>

    <div  id="xafaNsr_div" hidden>
        <table class="layui-hide" id="xafaNsr_table" lay-filter="xafaNsr_table" ></table>
    </div>
    <div class="panel panel-default operation_case_dataScope" hidden>
        <div class="panel-heading title"></div>
        <div class="layui-card-body">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input id="zxajId" name="zxajId" hidden/>
                    <button class="layui-btn" id="uploadExcel">请选择需要导入的Excel</button>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="btn_importTempDownload">导入模板下载</button>
                    <!--                    <button  class="layui-btn"  id="submit_importByAjbh" lay-filter="submit_importByAjbh">保存</button>-->
                    <button class="layui-btn layui-btn-primary" id="btn_cancel_imp">返回</button>
                </div>
            </div>
        </div>
    </div>


    <div id="nsrajfx_div" hidden>
        <div class="layui-tab table_div">
            <ul class="layui-tab-title">
                <li class="layui-this" id="fxbg" >分析报告</li>
                <li id="fxd" >风险点</li>
                <li id="jbxx" >基本信息</li>
                <li id="fpxx" >发票信息</li>
                <li id="cwxx" >财务信息</li>
                <li id="yqndaj" >以前年度案件</li>
            </ul>

            <div class="layui-tab-content">

                <div class="layui-tab-item layui-show">
                    <div id="fxbg_div" >
                    </div>
                </div>

                <div class="layui-tab-item">
                    <div id="fxd_div">
                    </div>
                </div>

                <div class="layui-tab-item">
                    <div id="jbxx_div" >
                    </div>
                </div>

                <div class="layui-tab-item">
                    <div id="fpxx_div" >
                    </div>
                </div>

                <div class="layui-tab-item">
                    <div id="cwxx_div" >
                    </div>
                </div>

                <div class="layui-tab-item">
                    <div id="yqndaj_div" >
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"  >添加</button>
<!--        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" >删除</button>-->
    </div>
</script>
<script type="text/html" id="tool">
    {{#  if(d.status === '0'||d.status === '61'||d.status === '62'||d.status === '63'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit" >修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="baopi" >报批</a>
    {{#  } else if(d.status === '11' || d.status === '12' || d.status === '13'  ){ }}
    <a class="layui-btn  layui-btn-checked layui-btn-xs" lay-event="chehui" >撤回</a>
    {{#  } else if(d.status === '2'){ }}
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="xuanan" >选案</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="daoru" >导入</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="zuofei" >作废</a>
    {{#  } else if(d.status === '3'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="zuofei" >作废</a>
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="lookMx" >查看明细</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="guidang" >归档</a>
    {{#  } else if(d.status === '4'){ }}
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="lookMx" >查看明细</a>
    {{#  } else if(d.status === '5'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
    {{#  } }}
</script>
<script type="text/html" id="nsrtool">
    <a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="ajfx" >案件分析</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var upload = layui.upload;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var startTime;
    var endTime;
    var importXafaId;
    layui.use(['table', 'layer', 'laydate','upload'], function () {
        // 字段类型映射
        var typeMap = {
            1: '双随机案件',
            2: '重点行业',
            3: '转交办',
            4:'其他'
        };
        var statusMap = {
            0:'已保存',
            11: '科长审批中',
            12: '主管局长审批中',
            13: '局长审批中',
            2: '已审批未选案',
            3: '已审批已选案',
            4:'已归档',
            5:'已作废',
            61:'科长审批不通过',
            62:'主管局长审批不通过',
            63:'局长审批不通过'
        };
        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/zbxaXafa/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            ,done: function(res, curr, count){
                // 数据加载完成后的回调函数
                // res 是后端返回的数据
                // 遍历数据，根据 showColumn 的值来控制是否显示某个列
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    var rowData = data[i];
                    if (rowData.status == '61') {
                        // 根据字段值动态隐藏某个列
                        // $(".layui-table-box").find("[data-field='showColumn']").addClass("layui-hide");
                        $(".layui-table-box").find("[data-field='kzReason']").removeClass("layui-hide");
                    }
                    if (rowData.status == '62') {
                        // 根据字段值动态隐藏某个列
                        // $(".layui-table-box").find("[data-field='showColumn']").addClass("layui-hide");
                        $(".layui-table-box").find("[data-field='zgjzReason']").removeClass("layui-hide");
                    }
                    if (rowData.status == '63') {
                        // 根据字段值动态隐藏某个列
                        // $(".layui-table-box").find("[data-field='showColumn']").addClass("layui-hide");
                        $(".layui-table-box").find("[data-field='jzReason']").removeClass("layui-hide");
                    }
                }
            }
            , cols: [
                [
                    // {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: 'id',hide: true},
                    {field: 'name', title: '方案名称'},
                    {field: 'type', title: '方案类型', templet: function(d){
                        return typeMap[d.type];
                    }},
                    // {field: 'startTime', title: '开始时间'},
                    // {field: 'endTime', title: '结束时间'},
                    {field: 'content', title: '方案文本'},
                    {field: 'bz', title: '备注'},
                    // {field: 'createTime', title: '创建时间'},
                    {field: 'status', title: '方案状态', templet: function(d){
                            return statusMap[d.status];
                        }},
                    {field: 'kzReason', title: '退回原因',hide: true},
                    {field: 'zgjzReason', title: '退回原因',hide: true},
                    {field: 'jzReason', title: '退回原因',hide: true},
                    { toolbar: "#tool", title: '操作',width:300}
                ]
            ]
            , toolbar: '#toolbar'
        });

        laydate.render({
            elem: '#startTime'
            , type: 'date'
            ,trigger:'click'
            , done: function (value, date, endDate) {
                if (value != null && value != undefined && value != "") {
                    startTime = value;
                } else {
                    startTime = null;
                }
            }
        });
        laydate.render({
            elem: '#endTime'
            , type: 'date'
            ,trigger:'click'
            , done: function (value, date, endDate) {
                if (value != null && value != undefined && value != "") {
                    endTime = value;
                } else {
                    endTime = null;
                }
            }
        });

        //初始化重点行业
        CoreUtil.sendPost("/zbxaXafa/getZdhyS", null, function(res) {
            let datas = res.data;
            let options = "";
            for (let i = 0; i < datas.length; i++) {
                options += '<option value="' + datas[i].id + '">' + datas[i].name + '</option>';
            }
            $('#typeZdhy').append(options);
            // 更新select渲染
            form.render('select');
        });
        //监听是否选择类型是否是重点行业
// 监听第一个下拉框的选中事件
        form.on('select(type)', function(data){
            var selectedValue = data.value;
            // 如果选中的值为 1，显示第二个下拉框，否则隐藏
            if(selectedValue === '2') {
                $('#zdhyContainer').show();
            } else {
                $('#zdhyContainer').hide();
            }
            // 重新渲染表单
            form.render();
        });
        //表头工具
        table.on('toolbar(showTable)', function(obj){
            switch(obj.event){
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if(data.length==0){
                        layer.msg("请选择要批量删除的列");
                    }else {
                        var ids = [];
                        $(data).each(function (index,item) {
                            ids.push(item.id);
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("新增");
                    $(".operation input[name=id]").val("");
                    $(".operation input[name=name]").val("");
                    $(".operation input[name=status]").val("");
                    $(".operation input[name=type]").val("");
                    $(".operation input[name=startTime]").val("");
                    $(".operation input[name=endTime]").val("");
                    $(".operation input[name=content]").val("");
                    $(".operation input[name=bz]").val("");
                    $(".operation input[name=lrrDm]").val("");
                    $(".operation input[name=swjgDm]").val("");
                    $(".operation input[name=createTime]").val("");
                    $(".operation input[name=updateTime]").val("");
                    break;
            };
        });
        //列操作
        table.on('tool(showTable)',function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'guidang':
                    layer.open({
                        content: "确定要归档吗?",
                        yes: function(index, layero){
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                            CoreUtil.sendPut("/zbxaXafa/guidang", {id:data.id},function (res) {
                                search();
                            });
                        }
                    });
                    break;
                case 'lookMx':
                    renderMxData(data.id);
                    layui.use('layer', function(){
                        layer.open({
                            type : 1,
                            skin : 'layui-layer-molv',
                            title : '方案涉及纳税人',
                            area : [ '100%', '100%' ],
                            shade : 0.8,
                            shadeClose : true,
                            content : jQuery("#xafaNsr_div")
                        });
                    });
                    break;
                case 'daoru':
                    layer.msg('暂未完成！');
                    importXafaId = data.id;
                    return false;
                    $(".operation_case_dataScope").show();
                    $("#showTable_div").hide();
                    // daoruDialog(data.id);
                    break;
                case 'baopi':
                    baopiDialog(data.id);
                    break;
                case 'xuanan':
                    skipOpen("/index/attention/xazb?xafaId=" + data.id, "80401", "指标选案");
                    break;
                case 'zuofei':
                    zuofeiDialog(data.id);
                    break;
                case 'chehui':
                    chehuiDialog(data.id);
                    break;
                case 'del':
                    var ids=[];
                    ids.push(data.id);
                    tipDialog(ids);
                    break;
                case 'edit':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("编辑");
                    $(".operation input[name=id]").val(data.id);
                    $(".operation input[name=name]").val(data.name);
                    $(".operation input[name=status]").val(data.status);
                    $(".operation input[name=type]").val(data.type);
                    $(".operation input[name=startTime]").val(data.startTime);
                    $(".operation input[name=endTime]").val(data.endTime);
                    $(".operation input[name=content]").val(data.content);
                    $(".operation input[name=bz]").val(data.bz);
                    $(".operation input[name=lrrDm]").val(data.lrrDm);
                    $(".operation input[name=swjgDm]").val(data.swjgDm);
                    $(".operation input[name=createTime]").val(data.createTime);
                    $(".operation input[name=updateTime]").val(data.updateTime);
                    break;
            }
        });
        //导入返回按钮
        $("#btn_cancel_imp").click(function () {
            $("#showTable_div").show();
            $(".operation_case_dataScope").hide();
            return false;
        });
        //导入模板下载
        $("#btn_importTempDownload").click(function () {
            var a = document.createElement('a');
            a.setAttribute('href', 'http://72.1.9.104/files/xatemplate.xlsx');
            a.setAttribute('target', '_blank');
            a.setAttribute('id', 'js_a');
            //防止反复添加
            if(document.getElementById('js_a')) {
                document.body.removeChild(document.getElementById('js_a'));
            }
            document.body.appendChild(a);
            a.click();
        });
        //导入  //@TODO 导入功能后将选案方案状态改为3 已审批已选案
        var uploadInst = upload.render({
            elem: '#uploadExcel'
            ,url:'/zbxaXafa/importByXafaId'
            ,data:{zxajId: function(){
                    return $("#zxajId").val();
                }}
            ,auto:true,
            // ,bindAction:'#btn_importTempDownload',
            accept:'file'
            ,acceptMime:'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ,exts:'xls|xlsx'
            ,before:function(obj){
                layer.load(); //上传loading
            }
            ,done: function(res, index, upload){
                let data = res.data;
                layer.alert("成功导入案件信息"+"【"+data+"】条");
                searchAjxx($("#zxajId").val());
                layer.closeAll('loading'); //关闭loaWding
            }
            ,error: function(){
                layer.alert("导入案件失败，请重试！");
                layer.closeAll('loading'); //关闭loading
            }
        });

        //监听弹出查看明细纳税人 的事件 案件分析
        table.on('tool(xafaNsr_table)',function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'ajfx':
                    layui.use('layer', function(){
                        layer.open({
                            type : 1,
                            skin : 'layui-layer-molv',
                            title : '案件分析',
                            area : [ '100%', '100%' ],
                            shade : 0.8,
                            shadeClose : true,
                            content : jQuery("#nsrajfx_div")
                        });
                    });
                    break;
            }
        });

        var renderMxData = function (id){
            layui.use('table', function(){
                table.render({
                    elem: '#xafaNsr_table'
                    , loading: false
                    ,contentType: 'application/json'
                    , headers: {"authorization": token}
                    ,where : {"id":id}
                    , url: '/attention/zbxaXafa/getNsrListByXafaId' //数据接口
                    , method: 'POST'
                    , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                        return {
                            "code": res.code, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                            "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                        }
                    }
                    , page: true //开启分页
                    , cols: [
                        [
                            {field: 'resname', title: '保存结果名称',width:200},
                            {field: 'djxh', title: '登记序号',width:200},
                            {field: 'nsrsbh', title: '纳税人识别号',width:200},
                            {field: 'nsrmc', title: '纳税人名称',width:200},
                            {field: 'shxydm', title: '社会信用代码',width:200},
                            {field: 'createTime', title: '数据生成时间',width:200},
                            {field: 'lrrDm', title: '数据生成人',width:200},
                            { toolbar: "#nsrtool", title: '操作',width:200,fixed : 'right'}
                        ]
                    ]
                    // , toolbar: '#toolbar'
                });
            });
        }
        //报批
        var baopiDialog=function (id) {
            layer.open({
                content: "确定要报批吗? 报批后将转给科长进行审批！",
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendPut("/zbxaXafa/baopi",{id:id},function (res) {
                        layer.msg(res.msg, {time:1000},function () {
                            search();
                        });
                    });
                }
            });
        };
        //作废
        var zuofeiDialog=function (id) {
            layer.open({
                content: "确定要作废吗?",
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendPut("/zbxaXafa/zuofei", {id:id},function (res) {
                        search();
                    });
                }
            });
        };
        //撤回
        var chehuiDialog=function (id) {
            layer.open({
                content: "确定要撤回吗?",
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendPut("/zbxaXafa/chehui", {id:id},function (res) {
                        search();
                    });
                }
            });
        };
        //导出
        $('#export').on('click', function () {
            //原先分页limit
            var exportParams = {
                limit: 10000,
                key: $("#key").val()
            };
            CoreUtil.sendPost("/zbxaXafa/listByPage", exportParams, function (res) {
                //初始化渲染数据
                if (res.data != null && res.data.records != null) {
                    table.exportFile(tableIns1.config.id, res.data.records, 'xls');
                }
            });
        });

        //删除
        var tipDialog=function (ids) {
            layer.open({
                content: "确定要删除吗?",
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/zbxaXafa/delete",ids,function (res) {
                        layer.msg(res.msg, {time:1000},function () {
                            search();
                        });
                    });
                }
            });
        };

        //返回
        $("#btn_cancel").click(function() {
            $(".table_div").show();
            $(".operation").hide();
            return false;
        });

        //监听保存
        form.on('submit(submit)', function(data){
            if(data.field.id===undefined || data.field.id===null || data.field.id===""){
                CoreUtil.sendPost("/zbxaXafa/add",data.field,function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            }else {
                CoreUtil.sendPut("/zbxaXafa/update",data.field,function (res) {
                    console.log(data.field);
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            }

            return false;
        });
    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                key: $("#key").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>